
import { addBanner, changeBanner, changebanner, delBanner, getBanner, getMember, oneBanner, oneMember } from "../../request/api";
import { successAlert } from '../../utils/alert'
const state = {
    tableData: [],//列表
    form: {
        title: "",
        img: "",
        status: 1
    },
    
}

// 2,声明mutations
const mutations = {
    // 专门来修改goods数据
    changebanner(state, obj) {
        state.tableData = obj;
    },
    editform(state, form) {
        state.form = form
    },

}

// 3.声明actions
const actions = {
    bannerActions(context) {
        getBanner().then(res=>{
            if(res.code==200){
                context.commit('changebanner',res.list)
            }
        })
    },
    oneActions(context, id) {
        oneBanner({id}).then(res=>{
            if(res.code==200){
                // console.log(res.list);
                res.list.id=id
                context.commit('editform',res.list)
            }
        })
    },
    updateActions(context, obj) {
        changeBanner(obj).then(res=>{
            if(res.code==200){
                successAlert(res.msg)
                // console.log(res.list);
                context.dispatch('bannerActions')

            }
        })
    },
    addActions(context, obj) {
        addBanner(obj).then(res => {
            if (res.code == 200) {
                successAlert(res.msg)
                // console.log(res);
                context.dispatch('bannerActions')

            }
        })
    },
    clearActions(context) {
        let form = {
            title: "",
            img: "",
            status: 1
        }
        context.commit('editform', form)
    },
    delActions(context,id){
        delBanner({id}).then(res=>{
            if(res.code==200){
                successAlert(res.msg)
                // console.log(res);
                context.dispatch('bannerActions')
            }
        })
    }


}
// 4.声明getters
const getters = {
    tableData(state) {
        return state.tableData
    },
    form(state) {
        return state.form
    },
}

export default {
    state,
    mutations,
    actions, getters,
    namespaced: true,//使用命名
}